<template>
  <div class="situation-data">
    <div class="data-head">
      <div class="data-name">平时考核（季度）情况</div>
    </div>
    <div class="data-top">
      <div class="top-li">
        <div class="top-li_top">
          <span class="name">考核部门数量</span>
        </div>
        <div class="top-li_bottom">
          <span>{{ sourceList.orgCount }}</span>
          个
        </div>
      </div>
      <div class="top-li">
        <div class="top-li_top">
          <span class="name">好等次人数</span>
        </div>
        <div class="top-li_bottom">
          <span>{{ sourceList.goodRankCount }}</span>
          人
        </div>
      </div>
      <div class="top-li">
        <div class="top-li_top">
          <span class="name">好等次占比</span>
        </div>
        <div class="top-li_bottom">
          <span>{{ sourceList.goodRankRate }}</span>
          %
        </div>
      </div>
    </div>
    <div class="data-bottom">
      <div class="charts" id="level_chart" v-if="xData.length > 0"></div>
      <div class="no_data" v-else>
        <empty></empty>
      </div>
    </div>
  </div>
</template>

<script>
import Echarts from '@libs/echarts'
export default {
  name: 'SituationData',
  components: {},
  props: {
    sourceList: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      myChart: null,
      xData: [],
      y1Data: [],
      y2Data: [],
    }
  },
  computed: {},
  watch: {
    sourceList: {
      handler(news) {
        if (news) {
          this.dealData(news)
        } else {
          this.xData = []
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  mounted() {
    // this.drawChart()
  },
  methods: {
    dealData(data) {
      if (data.orgRankList && data.orgRankList.length) {
        const xData = []
        const y1Data = []
        const y2Data = []
        data.orgRankList.forEach(item => {
          xData.push(item.orgName)
          y1Data.push(item.goodRankCount)
          y2Data.push(item.otherRankCount)
        })
        this.xData = xData
        this.y1Data = y1Data
        this.y2Data = y2Data
        this.$nextTick(() => {
          this.drawChart()
        })
      }
    },
    drawChart() {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = Echarts.init(document.getElementById('level_chart'))
      // 指定图表的配置项和数据
      const option = {
        title: {},
        legend: {
          itemWidth: 15,
          itemHeight: 15,
          data: ['好等次人数', '其他等次人数'],
        },
        xAxis: {
          data: this.xData,
          splitLine: {
            show: false,
          },
          axisLabel: {
            interval: 0,
            textStyle: {
              color: '#004DB6',
            },
            formatter: function (params) {
              let newParamsName = ''
              const paramsNameNumber = params.length
              const provideNumber = 4 // 一行显示几个字 然后就超过字数就会自动换行
              const rowNumber = Math.ceil(paramsNameNumber / provideNumber)
              if (paramsNameNumber > provideNumber) {
                for (let p = 0; p < rowNumber; p++) {
                  let tempStr = ''
                  const start = p * provideNumber
                  const end = start + provideNumber
                  if (p === rowNumber - 1) {
                    tempStr = params.substring(start, paramsNameNumber)
                  } else {
                    tempStr = params.substring(start, end) + '\n'
                  }
                  newParamsName += tempStr
                }
              } else {
                newParamsName = params
              }
              return newParamsName
            },
          },
        },
        yAxis: {
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed', // 设置网格线类型 dotted：虚线 solid:实线
            },
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            textStyle: {
              color: '#004DB6',
            },
          },
          name: '单位(人)',
          nameTextStyle: {
            color: '#004DB6',
          },
        },

        series: [
          {
            name: '好等次人数',
            type: 'bar',
            barWidth: 50, // 柱子宽度
            stack: '使用情况',
            data: this.y1Data,
            itemStyle: {
              normal: {
                color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: 'rgba(24, 144, 255, 0.5)' },
                  { offset: 1, color: 'rgb(24, 144, 255)' },
                ]),
              },
            },
          },
          {
            name: '其他等次人数',
            type: 'bar',
            barWidth: 50, // 柱子宽度
            stack: '使用情况',
            data: this.y2Data,
            itemStyle: {
              normal: {
                color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: 'rgba(30, 231, 231, 0.5)' },
                  { offset: 1, color: 'rgb(30, 231, 231)' },
                ]),
              },
            },
          },
        ],
        grid: {
          x: 40,
          y: 40,
          x2: 40,
          y2: 60,
        },
        // 移除指示线
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none',
          },
        },
      }
      // 使用刚指定的配置项和数据显示图表。
      this.myChart.setOption(option)
    },
  },
}
</script>

<style lang="scss" scoped>
.situation-data {
  .data-head {
    .data-name {
      color: #56585e;
      font-weight: bold;
      font-size: 16px;
    }
  }

  .data-top {
    margin-top: 20px;
    display: flex;
    align-items: center;

    .top-li {
      flex: 1;
      text-align: center;
      border-right: 1px solid rgba(142, 149, 178, 0.5);
      font-weight: 500;

      &:last-child {
        border: 0;
      }

      .top-li_top {
        font-size: 20px;

        .name {
          position: relative;
        }
      }

      .name::before {
        content: '';
        position: absolute;
        left: -20px;
        top: 50%;
        transform: translateY(-50%);
        width: 5px;
        height: 5px;
        border: 2px solid #4fa0ff;
        border-radius: 50%;
      }

      .top-li_bottom {
        margin-top: 10px;

        span {
          color: #333333;
          // font-weight: bold;
          font-size: 24px;
          margin-right: 4px;
        }
      }
    }
  }

  .data-bottom {
    margin-top: 10px;

    .charts {
      height: 250px;
      width: 100%;
    }
  }
}
</style>
